package com.example.imtoken.modules.main.wallet.select

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.StringUtils
import com.example.imtoken.R
import com.example.imtoken.core.OnSwitchWalletListener
import com.example.imtoken.entitys.app.ManageWalletsEntity
import com.example.imtoken.entitys.app.ManageWalletsTypeEntity
import com.example.imtoken.entitys.app.WalletsEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema
import java.util.*
import kotlin.Comparator

class SelectWalletDialogViewModel : ViewModel() {

    var isLoad = false

    private val manageWalletTypeData: MutableLiveData<MutableList<ManageWalletsTypeEntity>> =
        MutableLiveData()

    fun getManageWalletTypeLiveData(): MutableLiveData<MutableList<ManageWalletsTypeEntity>> {
        return manageWalletTypeData
    }

    private val manageWalletLiveData: MutableLiveData<MutableList<ManageWalletsEntity>> =
        MutableLiveData()

    fun getManageWalletLiveData(): MutableLiveData<MutableList<ManageWalletsEntity>> {
        return manageWalletLiveData
    }

    val switchWallet = MutableLiveData<OnSwitchWalletListener>()

    private var oldP = 0
    fun updateSelectData(position: Int) {
        if (oldP != position) {
            val data = manageWalletTypeData.value
            data?.get(oldP)?.isSelect = false
            data?.get(position)?.isSelect = true
            oldP = position
            manageWalletTypeData.value = data

            getManageWalletData(data?.get(position)?.walletType)
        }
    }

    private fun getManageWalletData(walletType: String? = "all") {
        val list = ArrayList<ManageWalletsEntity>()
        val walletsList = AppDataBase.instance.getAccountDao().getAllAccountWallet()
        when (walletType) {
            "all" -> {
                val manage1 = ManageWalletsEntity()
                manage1.manageName = StringUtils.getString(R.string.IdWallet)
                val wallets1 = ArrayList<WalletsEntity>()
                for (j in 0 until walletsList.size) {
                    if (walletsList[j].type == 1) {
                        val wallet = WalletsEntity()
                        wallet.name = walletsList[j].name
                        wallet.coin = walletsList[j].coin
                        wallet.key = walletsList[j].address
                        wallets1.add(wallet)
                    }
                }
                manage1.wallets = wallets1

                val manage2 = ManageWalletsEntity()
                manage2.manageName = StringUtils.getString(R.string.createOrImp)
                val wallets2 = ArrayList<WalletsEntity>()
                for (j in 0 until walletsList.size) {
                    if (walletsList[j].type != 1) {
                        val wallet = WalletsEntity()
                        wallet.name = walletsList[j].name
                        wallet.coin = walletsList[j].coin
                        wallet.key = walletsList[j].address
                        wallets2.add(wallet)
                    }
                }
                manage2.wallets = wallets2

                list.add(manage1)
                if (manage2.wallets?.size ?: 0 > 0) {
                    list.add(manage2)
                }
            }
            else -> {
                val manage = ManageWalletsEntity()
                manage.manageName = walletType
                val wallets = ArrayList<WalletsEntity>()
                for (j in 0 until walletsList.size) {
                    if ((walletsList[j].coin?.toUpperCase()) == walletType) {
                        val wallet = WalletsEntity()
                        wallet.name = walletsList[j].name
                        wallet.coin = walletsList[j].coin
                        wallet.key = walletsList[j].address
                        wallets.add(wallet)
                    }
                }
                manage.wallets = wallets
                list.add(manage)
            }
        }

        manageWalletLiveData.value = list
    }

    fun getManageWalletTypeData() {
        val walletsList = AppDataBase.instance.getAccountDao().getAllAccountWallet()
        val list = ArrayList<ManageWalletsTypeEntity>()
        val all = ManageWalletsTypeEntity(
            R.mipmap.src_lib_eui_icon_walletidentitynormal,
            R.mipmap.src_lib_eui_icon_walletidentity,
            isSelect = true
        )
        list.add(all)
        val set = TreeSet(Comparator<AccountSchema> { o1, o2 ->
            return@Comparator o1.coin?.compareTo(o2.coin ?: "")!!
        })
        set.addAll(walletsList)
        set.forEach {
            when (it.coin?.toUpperCase()) {
                "ETH" -> {
                    val eth = ManageWalletsTypeEntity(
                        R.mipmap.src_lib_eui_icon_walletethereumnormal,
                        R.mipmap.src_lib_eui_icon_walletethereum,
                        it.coin?.toUpperCase()
                    )
                    list.add(eth)
                }
                "BTC" -> {
                    val btc = ManageWalletsTypeEntity(
                        R.mipmap.src_lib_eui_icon_walletbchnormal,
                        R.mipmap.src_lib_eui_icon_walletbch,
                        it.coin?.toUpperCase()
                    )
                    list.add(btc)
                }
                "TRX" -> {
                    val trx = ManageWalletsTypeEntity(
                        R.mipmap.src_lib_eui_icon_wallettronnormal,
                        R.mipmap.src_lib_eui_icon_wallettron,
                        it.coin?.toUpperCase()
                    )
                    list.add(trx)
                }
                else -> {

                }
            }
        }
        manageWalletTypeData.value = list

        getManageWalletData()
    }

    fun switchHomeWallet(address: String?): Boolean {
        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
        appLogin.walletAddress = address
        return AppDataBase.instance.getAppLoginDao().updateAppLogin(appLogin) > 0
    }

}